<template>
  <el-dialog title="编辑付款账单" :visible.sync="dialogFormVisible">
    <el-form ref="editForm" :model="form" :rules="rules">
      <el-row :gutter="10">
        <el-col :span="12" v-if="billType == 1">
          <el-form-item label="订单编号">
            <span>{{form.bizNo}}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="billType == 2">
          <el-form-item label="同行名称">
            <span>{{form.userName}}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="用车时间">
            <span>{{form.startTime}}至{{form.endTime}}</span>
          </el-form-item>
        </el-col>
        <template v-if="billType == 1">

          <el-col :span="12">
            <el-form-item label="托管单价" prop="escrowUnitPrice">
              <el-input style="width: 230px" v-model="form.escrowUnitPrice" placeholder="请输入托管单价"></el-input>元/天
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用车天数" prop="usageDays">
              <el-input style="width: 230px" v-model="form.usageDays" placeholder="请输入用车天数"></el-input>天
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="托管费用" prop="custodyFees">
              <el-input style="width: 230px" v-model="form.custodyFees" placeholder="请输入托管费用"></el-input>元
            </el-form-item>
          </el-col>
        </template>

        <template v-if="billType == 2">
          <el-col :span="12">
            <el-form-item label="托管费用" prop="custodyFees">
              <el-input style="width: 230px" v-model="form.custodyFees" placeholder="请输入托管费用"></el-input>元
            </el-form-item>
          </el-col>
        </template>

      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button v-loading="loading"  type="primary" @click="confimEdit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {updateBill} from "@/api/bills/escrow"

export default {
  props: {},
  data() {
    // 这里存放数据
    return {
      loading:false,
      dialogFormVisible:false,
      billType:1, // 1:日租订单   2:车辆借出
      form:{},
      rules:{
        escrowUnitPrice: [
          {required: true, message: '请输入托管单价', trigger: 'change'},
          {
            pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
            message: '请输入合法的金额数字，最多两位小数',
            trigger: 'change',
          },
        ],
        usageDays: [
          {required: true, message: '请输入用车天数', trigger: 'change'},
          // {
          //   pattern: /^[1-9]\d*$/,
          //   message: '请输入正整数',
          //   trigger: 'change',
          // },
        ],
        custodyFees: [
          {required: true, message: '请输入托管费用', trigger: 'change'},
          {
            pattern: /^(([1-9]{1}\d*)|(0{1}))(\.\d{1,2})?$/,
            message: '请输入合法的金额数字，最多两位小数',
            trigger: 'change',
          },
        ],
      }
    }
  },
  // 计算属性 类似于 data 概念
  computed: {
  },
  watch:{
    'form.escrowUnitPrice' : {
      handler(value){
        if (this.form.usageDays && this.form.escrowUnitPrice) {
          this.form.custodyFees = Number(this.form.escrowUnitPrice) * Number(this.form.usageDays)
        }
      },
      // immediate:true,
      deep:true
    },
    'form.usageDays' : {
      handler(value){
        if (this.form.usageDays && this.form.escrowUnitPrice) {
          this.form.custodyFees = Number(this.form.escrowUnitPrice) * Number(this.form.usageDays)
        }
      },
      // immediate:true,
      deep:true
    },
  },
  // 方法集合
  methods: {
    show(item){
      this.dialogFormVisible = true;
      this.billType = item.billType;
      this.form = {
        ...item
      };
    },
    close(){
      this.dialogFormVisible = false;
    },
    confimEdit(){
      this.loading = true;
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          updateBill(this.form).then(res=>{
            this.$message({
              type: 'success',
              message: '编辑成功'
            });
            this.loading = false
            this.$emit('success')
            this.close()
          })
        }
      })
    }
  },
  // 生命周期 - 创建完成（可以访问当前this 实例）
  created() {
  },
  // 生命周期 - 挂载完成（可以访问 DOM 元素）
  mounted() {
  },

}
</script>

<style lang="scss" scoped>
</style>
